package com.hsgene.version.service.impl;

import com.hsgene.common.util.date.SimpleDateUtils;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.common.util.validation.FieldChecker;
import com.hsgene.generator.dto.GeneratorDto;
import com.hsgene.version.domain.VersionDomain;
import com.hsgene.version.dto.VersionDto;
import com.hsgene.version.exception.VersionErrorCode;
import com.hsgene.version.persistence.VersionRepository;
import com.hsgene.version.service.VersionService;
import com.hsgene.version.service.api.GeneratorService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: //TODO
 * @ProjectName: hdas-geneshop
 * @Package: com.hsgene.version.service.impl
 * @Author: 江承良
 * @CreateDate: 2019/1/18 15:08
 * @Version: 1.0
 * Copyright: Copyright (c) 2019
 */
@Service
public class VersionServiceImpl implements VersionService {
    @Resource
    private VersionRepository versionRepository;
    @Resource
    private GeneratorService generatorService;

    /***
     * 添加产品更新内容
     * @author jcl
     * @date 2019/1/18 10:12
     * @param versionDomain
     * @return
     */
    @Override
    public ApiResult addVersion(VersionDomain versionDomain) {
        //验证版本号是否已经存在
        VersionDto versionDto = versionRepository.versionByVersionNo(versionDomain.getVersionNo());
        if (versionDto != null) {
            return ApiResult.fail(VersionErrorCode.VERSION_NUMBER_REPEAT);
        }
        ApiResult<GeneratorDto> generator = generatorService.getGenerator("genetic_testing_version", null);
        versionDomain.setId(generator.getData().getSerialNumber());
        //判断发布日期>=当前日期？
        Date nowDate = SimpleDateUtils.getDateOfMidNight();
        if (versionDomain.getReleaseDateTime() != null) {
            if (versionDomain.getReleaseDateTime().getTime() < nowDate.getTime()) {
                return ApiResult.fail(VersionErrorCode.DATE_LESS_THAN_NOW);
            }
        }
        versionDomain.setCreateDateTime(new Date());
        versionDomain.setUpdateDateTime(new Date());
        versionRepository.addVersion(versionDomain);
        return ApiResult.succ();
    }

    /***
     * 产品更新列表查询
     * @author jcl
     * @date 2019/1/18 11:34
     * @param limitHelper 分页条件
     * @param versionNo 版本号
     * @param content 内容
     * @param releaseDateTime 发布时间
     * @return
     */
    @Override
    public ApiResult versionPages(LimitHelper limitHelper, String versionNo, String content, String orderField, Date releaseDateTime) {
        FieldChecker.checkEmpty(limitHelper, "limitHelper");
        int count = versionRepository.versionCount(versionNo, content, releaseDateTime);
        Pagination<VersionDto> pagination = Pagination.newInstance(limitHelper, count);
        List<VersionDto> versionDtos = new ArrayList<>();
        if (count > 0) {
            versionDtos = versionRepository.versionPages(limitHelper, versionNo, content, orderField, releaseDateTime);
            if (versionDtos != null && versionDtos.size() > 0) {
                int rowNum = (limitHelper.getPage() - 1) * limitHelper.getSize();
                for (int i = 0; i < versionDtos.size(); i++) {
                    VersionDto versionDto = versionDtos.get(i);
                    rowNum = rowNum + i + 1;
                    versionDto.setNumber(rowNum);
                }
            }
        }
        pagination.setResult(versionDtos);
        return new ApiResult(200, "success", "列表获取成功", pagination);
    }

    /***
     * 查询产品更新具体内容
     * @author jcl
     * @date 2019/1/18 14:20
     * @param id
     * @return
     */
    @Override
    public ApiResult versionById(String id) {
        return new ApiResult(200, "success", "数据获取成功", versionRepository.versionById(id));
    }

    /***
     * 更新内容
     * @author jcl
     * @date 2019/1/18 15:53
     * @param versionDomain
     * @return
     */
    @Override
    public ApiResult updateVersion(VersionDomain versionDomain) {
        //验证版本号是否已经存在
        VersionDto versionDto = versionRepository.versionByVersionNo(versionDomain.getVersionNo());
        if (versionDto != null && !versionDomain.getId().equals(versionDto.getId())) {
            return ApiResult.fail(VersionErrorCode.VERSION_NUMBER_REPEAT);
        }
        //判断发布日期>=当前日期？
        /*Date nowDate = SimpleDateUtils.getDateOfMidNight();
        if (versionDomain.getReleaseDateTime() != null) {
            if (versionDomain.getReleaseDateTime().getTime() < nowDate.getTime()) {
                return ApiResult.fail(VersionErrorCode.DATE_LESS_THAN_NOW);
            }
        }*/
        versionDomain.setUpdateDateTime(new Date());
        versionRepository.updateVersion(versionDomain);
        return new ApiResult(200, "success", "更新成功", null);
    }

    /***
     * 删除
     * @author jcl
     * @date 2019/1/18 16:10
     * @param id
     * @return
     */
    @Override
    public ApiResult deleteVersion(String id) {
        versionRepository.deleteVersion(id);
        return new ApiResult(200, "success", "删除成功", null);
    }

    /***
     *根据版本号查询产品更新详情
     * @author jcl
     * @date 2019/1/18 16:18
     * @param versionNo
     * @return
     */
    @Override
    public ApiResult versionByVersionNo(String versionNo) {
        return new ApiResult(200, "success", "数据获取成功", versionRepository.versionByVersionNo(versionNo));
    }

    /**
     * @param versionNo 版本号
     * @return com.hsgene.version.dto.VersionDto
     * @description 根据版本号模糊查询更新日志版本信息
     * @author maodi
     * @createDate 2019/1/21 10:24
     */
    @Override
    public List<VersionDto> getVersionsLogs(String versionNo) {
        List<VersionDto> list = versionRepository.getVersionsLogs(versionNo);
        int num = 1;
        for (VersionDto vd : list) {
            vd.setNumber(num++);
        }
        return list;
    }

    /**
     * @param
     * @return java.util.List<java.lang.String>
     * @description 查询所有版本号
     * @author maodi
     * @createDate 2019/1/21 10:32
     */
    @Override
    public List<String> getVersionsNos() {
        return versionRepository.getVersionsNos();
    }

    /***
     * 检查版本号是否重复
     * @author jcl
     * @date 2019/1/29 10:19
     * @param versionNo 版本号
     * @param versionId 版本id
     * @return
     */
    @Override
    public ApiResult versionNoRepeat(String versionNo, String versionId) {
        VersionDto versionDto = versionRepository.versionByVersionNo(versionNo);
        if(versionDto!=null&&!versionDto.getId().equals(versionId)){
            return new ApiResult(200,"success","版本号已存在",1);
        }
        return new ApiResult(200,"success","版本号不重复",0);
    }
}
